<?php
class Locality_Agent extends WP_Widget {
  
  	function Locality_Agent() {	
  		$widget_ops = array( 'classname' => 'locality_agent', 'description' => __("Display agnet name, picture and short desicription", 'locality') );
  		$this->WP_Widget( 'locality_agent', 'Locality: Agent Widget', $widget_ops);
  	}
  	
  	function form($instance) {
  			
  		$instance = wp_parse_args( (array) $instance, array('name' => 'John Doe', 'pic' => '#', 'agent_url' => '#' , 'mobile'=>'' , 'office'=>'' , 'fax'=>'' , 'desc' => 'Some description text') );
  
        $name = esc_attr($instance['name']);
        $pic = esc_attr($instance['pic']);
  		$agent_url = esc_attr($instance['agent_url']);	
		$mobile = esc_attr($instance['mobile']);
		$office = esc_attr($instance['office']);
		$fax = esc_attr($instance['fax']);
  		$desc = esc_attr($instance['desc']);
  
  		?>
  				<p>
  		            <label for="<?php echo $this->get_field_id('name'); ?>">
  		             <?php _e( 'Name:', 'locality') ?>
  		            </label>
  		                <input class="widefat" id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" type="text" value="<?php echo $name; ?>" />
  		        </p>
  		
  				
  		
  				<p>
  		            <label for="<?php echo $this->get_field_id('pic'); ?>">
                       <?php _e( 'Agent Pic URL:', 'locality') ?>
  		            </label>
  		            <input class="widefat" id="<?php echo $this->get_field_id('pic'); ?>" name="<?php echo $this->get_field_name('pic'); ?>" type="text" value="<?php echo $pic; ?>" />
  		                
  		        </p>
                  
                <p>
  		            <label for="<?php echo $this->get_field_id('agent_url'); ?>">
                       <?php _e( 'Target URL:', 'locality') ?>
  		            </label>
  		            <input class="widefat" id="<?php echo $this->get_field_id('agent_url'); ?>" name="<?php echo $this->get_field_name('agent_url'); ?>" type="text" value="<?php echo $agent_url; ?>" />
  		                
  		        </p>
  				
				<p>
  		            <label for="<?php echo $this->get_field_id('mobile'); ?>">
                       <?php _e( 'Mobile:', 'locality') ?>
  		            </label>
  		            <input class="widefat" id="<?php echo $this->get_field_id('mobile'); ?>" name="<?php echo $this->get_field_name('mobile'); ?>" type="text" value="<?php echo $mobile; ?>" />
  		                
  		        </p>
				
				<p>
  		            <label for="<?php echo $this->get_field_id('office'); ?>">
                       <?php _e( 'Office:', 'locality') ?>
  		            </label>
  		            <input class="widefat" id="<?php echo $this->get_field_id('office'); ?>" name="<?php echo $this->get_field_name('office'); ?>" type="text" value="<?php echo $office; ?>" />
  		                
  		        </p>
				
				<p>
  		            <label for="<?php echo $this->get_field_id('fax'); ?>">
                       <?php _e( 'Fax:', 'locality') ?>
  		            </label>
  		            <input class="widefat" id="<?php echo $this->get_field_id('fax'); ?>" name="<?php echo $this->get_field_name('fax'); ?>" type="text" value="<?php echo $fax; ?>" />
  		                
  		        </p>				
				
  				<p>
  		
  				<p>
  		            <label for="<?php echo $this->get_field_id('desc'); ?>">
                       <?php _e( 'Agent Description:', 'locality') ?>
  		            </label>
  		            <textarea class="widefat" id="<?php echo $this->get_field_id('desc'); ?>" name="<?php echo $this->get_field_name('desc'); ?>" cols="10" rows="5" ><?php echo $desc; ?></textarea>
  		        </p>	
  		
  		<?php
      }
  
  	function update($new_instance, $old_instance) 
  	{
		$instance=$old_instance;
  		
		$instance['name'] = strip_tags($new_instance['name']);
		$instance['pic'] = strip_tags($new_instance['pic']);
		$instance['agent_url'] = strip_tags($new_instance['agent_url']);
		$instance['mobile'] = strip_tags($new_instance['mobile']);
		$instance['office'] = strip_tags($new_instance['office']);
		$instance['fax'] = strip_tags($new_instance['fax']);
		$instance['desc'] = strip_tags($new_instance['desc']);
		
		return $instance;  
	}
  
  	function widget($args, $instance) {
  	
  		extract($args);
  
        $name = $instance['name'];
  		$pic = $instance['pic'];
  		$agent_url = $instance['agent_url'];
  		$desc = $instance['desc'];
		$mobile = $instance['mobile'];
		$office = $instance['office'];
		$fax = $instance['fax'];
  		
  		?>
          <div class="side-widget">
                  <div class="agent-container clearfix">
                          <div class="agent-pic-wrapper">
                                <a href="<?php echo $agent_url; ?>"><img src="<?php echo $pic; ?>" alt="agent" /></a>
                          </div>
                          <h4><a href="<?php echo $agent_url; ?>"><?php echo $name; ?></a></h4>
						  <div class="agent-info">
						  	<?php
							if(!empty($mobile))
							{
								?>
							  	<span class="mobile"><?php _e('Mobile','locality'); ?>: <?php echo $mobile;?></span>
								<?php
							}
							if(!empty($office))
							{
								?>
								<span class="office"><?php _e('Office','locality'); ?>: <?php echo $office;?></span>
								<?php
							}
							if(!empty($fax))
							{
								?>
								<span class="fax"><?php _e('Fax','locality'); ?>: <?php echo $fax;?></span>
								<?php
							}							
							?>
						  </div>
                          <p><?php echo $desc; ?></p>
                  </div>
          </div>
          <?php
  	}
  
}